﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
using UnityEngine.SceneManagement;
using System;
using QxFramework.Utilities;
public class LevelManager : Singleton<LevelManager> , ISystemModule
{
    //加载进度
    public float LoadProgress
    {
        get
        {
            if (_asyncOperation == null)
            {
                return _asyncOperation.progress;
            }
            else
            {
                return 1f;
            }
        }
    }

    public string CurrentLevel
    {
        get
        {
            return _currentLevel;
        }

        set
        {
            _currentLevel = value;
        }
    }
  /*  public string LastLevel
    {
        get
        {
            return Data.Instance.Table.CollectKey1("LevelSetting")[0];
        }
    }
    */
    public string FirstLevel
    {
        get
        {
            return Data.Instance.Table.CollectKey1("LevelSetting")[0];
        }
    }
    /// <summary>
    /// 如果为最后一关则重启当前关卡
    /// </summary>
    public string NextLevel
    {
        get
        {
            var key = Data.Instance.Table.CollectKey1("LevelSetting");
            int index = key.LastIndexOf(LevelManager.Instance.CurrentLevel) + 1;
            if (index < key.Count && Data.Instance.Table.GetInt("LevelSetting", key[index], "Lock") == 0)
            {
                return key[index];
            }
            else
            {
                return _currentLevel;
            }
        }
    }

    private string _currentLevel = string.Empty;

    private Action<string> _onCompleted = null;

    private AsyncOperation _asyncOperation = null;

    public void OpenLevel(string levelName, Action<string> onCompleted)
    {
        if (_onCompleted != null)
        {
            Debug.LogError("[LevelManager] 已经正在加载一个关卡中......");
        }

        if (string.IsNullOrEmpty(levelName))
        {
            levelName = CurrentLevel;
            CloseLevel();           
        }
//        if(SceneManager.GetSceneByName(levelName).isLoaded
        CurrentLevel = levelName;
        _onCompleted = onCompleted;
        _asyncOperation = SceneManager.LoadSceneAsync(CurrentLevel,LoadSceneMode.Additive);       
    }

    public void CloseLevel()
    {
        if (!string.IsNullOrEmpty(CurrentLevel))
        {
            UIManager.Instance.CloseAll();
            ObjectPool.DestroyAllPooled();
            SceneManager.UnloadSceneAsync(CurrentLevel);
         //       .completed+=(AsyncOperation a)=> {
      //          ObjectPool.DestroyAllPooled();
        //    };
            CurrentLevel = string.Empty;
        }
    }

    public override void Initialize()
    {
        SceneManager.sceneLoaded += LoadCompleted;
       /* SceneManager.sceneUnloaded += (Scene scene) =>
        {
            ObjectPool.DestroyAllPooled();
        };*/
    }

    private void LoadCompleted(Scene scene, LoadSceneMode loadSceneMode)
    {
        SceneManager.SetActiveScene(scene);
        if (_onCompleted !=null)
        {
            _onCompleted(scene.name);
            _onCompleted = null;
        }
    }

    public void Update(float deltaTime)
    {

    }

    public void FixedUpdate(float deltaTime)
    {

    }

    public void Dispose()
    {
        SceneManager.sceneLoaded -= LoadCompleted;
    }
}
